package com.levy.utils.provider.impl;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import com.levy.utils.provider.ParamProvider;


public class SessionParamProvider extends ParamProvider {
	
	private static final Logger log = Logger.getLogger(SessionParamProvider.class);
	private HttpServletRequest request;
	public static final String SESSION_MAP = "session_all";
	
	@SuppressWarnings("unchecked")
	public SessionParamProvider(HttpServletRequest request){
		super(request);
		this.request = request;
		HttpSession session = this.request.getSession(false);
		if(session!=null){
			Map<String, Object> ps = (Map<String, Object>) session.getAttribute(SESSION_MAP);
			if(ps!=null)this.params = ps;
			else log.warn("session失效！");
		}else log.warn("session失效！");
	}

	@Override
	public void setParameter(String key, Object value) {
		HttpSession session = this.request.getSession(false);
		if(session!=null){
			if (params.containsKey(key))
				log.warn("已经有一个名为：'" + key + "'，值为：'" + params.get(key) + "'的参数存在。该参数将被覆盖");
			params.put(key, value);	
			session.setAttribute(SESSION_MAP, this.params);		
		}else {
			log.warn("加入的session参数名为：'" + key + "'，值为：'" + value + "'，因为session失效而加入失败");
		}
	}

}

